#  Copyright (c) 2007 Microsoft Corporation. All rights reserved.
$:.unshift File.join(File.dirname(__FILE__),"..")
require 'erb'
require 'rubygems'
require 'vmxt2tfs/templates'

module VMXT2TFS
  #Base class for the objects created from the VM-XT xml elements.
  class VMXTElement
    attr_accessor :number,:description,:title,:vmxt_ref,:vmxt_type
    attr_writer :iteration
    #Initialization keys are 
    # :number - The element's number 
    # :description - Description string
    # :title - Title string (the Name element)
    # :vmxt_ref - VM-XT ID
    # :vmxt_type - the type of the element
    def initialize params
      @title=params[:title]
      @description=params[:description]
      @description||=""
      @number=params[:number]
      @vmxt_ref=params[:vmxt_ref]
      @vmxt_type=params[:vmxt_type]
      @params=params
      raise "Missing parameter. :vmxt_ref is required" unless @vmxt_ref
    end
    #returns a Hash with the initialization parameters
    def parameters
      return @params
    end
    def == other
      return true if @vmxt_ref==other.vmxt_ref
      return false
    end
    #generates the WI snippet for this instance.
    #
    #__template__ is the ERB template to use for the snippet
    def snippet template
      return ERB.new(template).result(self.get_binding)
    end
    #Support templating of member data.
    def get_binding
      binding
    end
  end
  #This is the in-memory representation of Entscheidungspunkt.
  class Entscheidungspunkt<VMXTElement
  end
  #This is the in-memory representation of Meilenstein.
  class Meilenstein<VMXTElement
    attr_accessor :ep_ref  
    #Initialization keys are 
    # :ep_ref - the Entscheidungspunkt reference id
    def initialize params
      super(params)
      @ep_ref=params[:ep_ref]
      @end_date=params[:end_date]
    end
    #generates the default WI snippet for this instance
    def snippet template=VMXT2TFS::Templates::DEFAULT_EPUNKT
      super(template)
    end

    def end_date
      @end_date.strftime("%d/%m/%y")
    end

    def iteration
      return "$$PROJECTNAME$$" unless @params[:iteration]
      return "$$PROJECTNAME$$" if @params[:iteration].empty?
      return "$$PROJECTNAME$$\\#{@params[:iteration]}"
    end
  end
  #This is the in-memory representation of the Aktivitaet type defined in VM-XT.
  class Aktivitaet<VMXTElement
    attr_accessor :product_ref,:group
    #Initialization keys are 
    # :product_ref - the reference id of the activity in VM-XT 
    # :group - the name of the activity group this activity belongs to
    def initialize params
      super(params)
      @product_ref=params[:product_ref]
      @group=params[:group]
    end
    #Returns the area path for the WI
    def area
      return "$$PROJECTNAME$$\\#{@group}"
    end
  end
  #Represents an instance of Aktivitaet as defined in the VMP plan of the tailored process.
  class AktivitaetInstanz<Aktivitaet
    attr_accessor :milestone
    #AktivitaetInstanz can be initialized by passing an Aktivitaet instance with the :activity key.
    #
    # :milestone - The Meilenstein instance to which this activity instance belongs
    def initialize params
      if params[:activity]
        super(params[:activity].parameters)
      else
        super(params)
      end
      @milestone=params[:milestone]
    end
    def milestone_ref
      return @milestone.vmxt_ref
    end
    def end_date
      return @milestone.end_date
    end
    def iteration
      return @milestone.iteration
    end
    #Generates the TFS XML to be inserted in 
    #WorkItems.xml
    def snippet template=VMXT2TFS::Templates::DEFAULT_AKTIVITAET
      super(template)
    end
    def == other
      return true if @vmxt_ref==other.vmxt_ref && @milestone_ref==other.milestone_ref
      return false
    end
  end
  #This is the in-memory representation of Produkt.
  class Produkt<VMXTElement
    attr_accessor :ep_refs,:group
    attr_writer :initial
    #Initialization keys are 
    # :ep_refs - an Array of reference ids of all the Entscheidungpunkt instances refering to this product
    # :group - the name of the product group this activity belongs to
    # :initial - boolean value determining if it's an initial product or not
    def initialize params
      super(params)
      @ep_refs=params[:ep_refs]
      @initial= (params[:initial]==true)
      @group=params[:group]
    end
    #Returns true if this is an initial product
    def initial?
      return @initial
    end
    #Returns the area path for the WI
    def area
      return "$$PROJECTNAME$$\\#{@group}"
    end
    def iteration
      return "$$PROJECTNAME$$"
    end
    #Generates the TFS XML to be inserted in 
    #WorkItems.xml
    def snippet template=VMXT2TFS::Templates::DEFAULT_PRODUKT
      super(template)
    end
  end
  #Report represents a TFS report.
  #
  #It is used in creating the ReportsTasks.xml file
  class Report
    attr_accessor :datasources, :parameters, :name, :filename, :folder, :expiration
    #Initialization keys are 
    # :name - the name of the report
    # :filename - the filename of the report file
    # :folder - folder name
    # :expiration - the cache expiration time
    # :parameters - an Array of {:name,:value}
    # :datasources - an array of {:name, dnsname}
    def initialize params
      @name=params[:name]
      @filename=params[:filename]
      @folder=params[:folder]
      @expiration=params[:expiration]
      @datasources=params[:datasources]
      @parameters=params[:parameters]
    end
    #generates the XML snippet for this report.
    #
    #__template__ is the ERB template to use for the snippet
    def snippet template=VMXT2TFS::Templates::REPORT
      return ERB.new(template).result(self.get_binding)
    end
    #Support templating of member data.
    def get_binding
      binding
    end
  end
end